package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.situ.mall.mapper.ShippingMapper;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.vo.CartListVO;
import com.situ.mall.pojo.vo.OrderListVO;
import com.situ.mall.service.ICartService;
import com.situ.mall.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.service.IOrderService;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class FrontOrderController {
	@Autowired
	private IOrderService orderService;
	@Autowired
	private ICartService cartService;

	// 转到订单确认页面
	@RequestMapping("/getOrder.shtml")
	public String getOrder(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		// 用户收货地址信息
		List<Shipping> shippinglist = orderService.selectByUserId(user.getId());
		// 用户购物车被选中信息
		List<CartListVO> cartVOList = cartService.selectCheckByUser(user.getId());
		model.addAttribute("shippinglist", shippinglist);
		model.addAttribute("cartVOList", cartVOList);
		return "front/order";
	}

	// 生成订单，并将对应购物车中的商品移除购物车
	@ResponseBody
	@RequestMapping("/insert.shtml")
	public JSONResult insert(HttpSession session, Order order) {
		User user = (User) session.getAttribute("user");
		order.setUserId(user.getId());
		JSONResult jsonResult = orderService.addToOrder(order);
		return jsonResult;
	}

	// 跳转到支付页面
	@RequestMapping("/getPayPage.shtml")
	public String getPayPage(Order order, Model model) {
		OrderListVO orderListVO = orderService.selectByOrderNo(order.getOrderNo());
		System.out.println(orderListVO);
		model.addAttribute("orderVO", orderListVO);
		return "front/pay";
	}

	// 取消订单
	@ResponseBody
	@RequestMapping("/deleteByOrderNo.shtml")
	public JSONResult deleteByOrderNo (Order order) {
		JSONResult jsonResult = orderService.deleteByOrderNo(order.getOrderNo());
		return jsonResult;
	}
}
